home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1201 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: typedef double Poly[MAXPOLY] PROBLEM
  5. Date: 12 Jan 1996 04:02:50 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d4mha$pf7@news.iag.net>
  8. References: <4d2leh$14dm@pulp.ucs.ualberta.ca>
  9. NNTP-Posting-Host: pm3-orl6.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4d2leh$14dm@pulp.ucs.ualberta.ca>, ryangall@gpu.srv.ualberta.ca 
  13. says...
  14. >
  15. >how can I send a (pointer to Poly) to a function, and access each index 
  16. >for updating?  this is my definition....its for school, and the typedef 
  17. >double poly[MAXDEGREE] cannot be changed.....heres an example of one of 
  18. >the functions I tried. It bails out at n=4 ...probably cause thats the 
  19. >size of the pointer......it keeps crashing
  20. >
  21. >
  22. >#define MAXDEGREE 200
  23. >
  24. >typedef double Poly[MAXDEGREE];
  25. >typedef Poly * POLY;
  26. >
  27. >/* initialize the Poly P so all index's are 0 */
  28. >
  29. >int initPoly(POLY P)
  30. >{
  31. > int n;
  32. > for(n=0; n<MAXDEGREE; n++)
  33. > {
  34. >   *P[n]=0.0;  /* can you see what Im trying to do here?!*/
  35.  
  36. Your problem is with operator precedence.  The array member operator '[]'
  37. has a higher precedence than the dereference operator '*'.  So this is
  38. equivalent to:
  39.  
  40.    *(P[n])=0.0; 
  41.  
  42. Use a debugger to step through your loop. Watch the following values: 
  43.  
  44. n    /* your index                                                     */
  45. (*P) /* or P[0]: the address of the array you defined in main          */
  46. P[n] /* or &(*P[n]): the address you are current trying to dereference */
  47. <You can fake this by inserting strategic printfs and getchars in the loop>
  48.  
  49. On the second iteration (when n == 1).  Subtract the value of (*P) from
  50. P[n].  You will end up with a figure equal to 200 * sizeof(double).  You
  51. are literally indexing across arrays of 200 doubles, instead of through the
  52. elements of your intended array.  So, after the first assignment, you are 
  53. overwriting something you don't intend to (probably part of your stack).
  54.  
  55. Use parens to force the precedence you need (ie, dereference P, to obtain
  56. your Poly, then index it).
  57.  
  58. -- 
  59. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  60. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  61.  
  62.